package com.yej.aiPolice.ui.fragment

import android.os.Bundle
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.baidu.ai.aip.faceAPI.FaceServiceImpl
import com.baidu.ai.aip.faceDatabase.FaceDatabaseServiceImpl
import com.yej.aiPolice.R
import com.yej.aiPolice.presenter.SearchFragmentPresenter
import org.jetbrains.anko.find
import java.io.File
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors


class SearchFragment : Fragment(), View.OnClickListener {

    private var tvShow: TextView? = null
    private var ivShow: ImageView? = null
    //用来转换相机路径用的
//    private var localMedia: LocalMedia = LocalMedia()
    //所选相册图片的路径(原图/压缩后/剪裁后)
    private var albumPath: String = ""

    private var faceDatabaseService = FaceDatabaseServiceImpl()

    private var faceService = FaceServiceImpl()

    private var searchFragmentPresenter = SearchFragmentPresenter(this)
    //单线程化线程池

    private val executorService: ExecutorService = Executors.newSingleThreadExecutor();

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.activity_search_info, null)
        initView(view)
        return view
    }

    private fun initView(view: View) {
        val btCamera =
            view.findViewById<Button>(R.id.btn_photo)
        ivShow = view.find(R.id.iv_search_face)
        btCamera.setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        /**
         * create方法参数一是上下文，在activity中传activity.this，在fragment中传fragment.this。参数二为请求码，用于结果回调onActivityResult中判断
         * selectPicture方法参数分别为图片的裁剪宽、裁剪高、宽比例、高比例。默认不传则为宽200，高200，宽高比例为1：1。
         */
//        PictureSelector.create(this)
//            .openCamera(PictureMimeType.ofImage()).imageFormat(PictureMimeType.PNG)
//            .loadImageEngine(GlideEngine.createGlideEngine()) // 请参考Demo GlideEngine.java
//            .forResult(PictureConfig.REQUEST_CAMERA)


    }


    // 压缩后图片文件存储位置
    private fun getCompressPath(): String {
        var path: String =
            Environment.getExternalStorageDirectory().getAbsolutePath() + "/PictureSelector/image/"
        var file: File = File(path)
        if (file.mkdirs()) {
            return path
        }
        return path
    }

//    @SuppressLint("RestrictedApi")
//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//        Log.e("order", "回调，返回码$resultCode")
//        if (resultCode == -1) { // 如果返回码是可以用的
//            Log.e("order", "返回码可以用")
//            when (requestCode) {
//                PictureConfig.REQUEST_CAMERA ->
//                    if (PictureSelector.obtainMultipleResult(data).get(0) != null) {
//                        Log.e("order", "获得相片路径")
//                        localMedia = PictureSelector.obtainMultipleResult(data).get(0)
//                        albumPath = localMedia.androidQToPath
//                        var bitMap = IOUtil.readBitmapFromFileDescriptor(albumPath, 200, 200)
//                        ivShow?.setImageBitmap(bitMap)
//                        searchFragmentPresenter.searchFace(albumPath)




//                        Log.e("order", albumPath)

//                        var postThread = Thread(Runnable {
////                               var result:FaceAddBean =
////                                faceDatabaseService.add(albumPath, "testALL","ttt","ttt") as FaceAddBean
////
//                            var result = faceService.faceSearch(albumPath) as FaceSearchBean
//                            Looper.prepare();
//                            if(result == null) {
//                                context?.toast("代码错误")
//                                Log.e("order","代码有误")
//                            }else{
//                                if(result.result == null){
//                                    context?.toast("照片中不存在人脸")
//                                }else{
//                                    var score =  result.result.user_list[0].score
////                                人脸搜索阈值
//                                    if(score>80){
//                                        context?.toast("人脸搜索${result.error_msg} 匹配度${score}")
//                                        Log.e("order","算法调用${result.error_msg}")
//                                        Log.e("order","人脸信息${result.result.user_list[0].user_info}")
//                                    }else{
//                                        context?.toast("人脸搜索失败  最高匹配度${score}")
//                                    }
//                                }
//
//                            }
//                            Log.e("order","调用完成")
//                            Looper.loop();
//                        })
//                        postThread.start()

//
//                    }
//
//            }
//        }
//
//    }
//                    }
//
//            }
//        }
//
//    }

    fun errorCode() {

    }

    fun toast(s: String) {
//        context?.toast(s)
    }

}
